<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <!-- 上级标准 -->
        <h2 style='font-size: 16px;font-weight: bold;'>评分标准</h2>
        <div style='padding-top: 0px;'>
          <a-row>
            <a-col :span="12">
              <a-form-model-item label="所属试卷" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paperId">
                <a-input v-model="standardModel.paperId" placeholder="请输入所属试卷" disabled></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="上级标准" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="achievementStandardId">
<!--                <a-input v-model="standardModel.achievementStandardId" placeholder="请选择所属上级标准"  ></a-input>-->
                <a-select v-model="achievementStandardId" placeholder="请选择所属上级标准"
                          :getPopupContainer="(target) => target.parentNode" >
                  <a-select-option v-for="item in standardList" :key="item.value">{{ item.label }}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
          </a-row>
        </div>
        <!-- 上级标准 -->
        <!-- 明细标准 -->
        <div style='margin-top: 10px;font-size: 14px;font-weight: bold;'><a-icon type="right"/> 添加分项标准<span style='font-size: 14px;font-weight: lighter;'> （至少要有一项标准分项数据）</span></div>
        <div style='background: #eeeeee;border-radius: 6px;margin-top: 10px;padding-top: 10px;' v-for="(item, index) in orderMainModel.mainCustomerList" :key="index" >
          <a-row>
            <a-col :span="12">
              <a-form-model-item label="Code(试卷分项)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="exaPapProItemCode">
<!--                <a-input v-model="item.exaPapProItemCode" placeholder="请输入项目code(来源于项目模板或项目分配表）"></a-input>-->
                <j-dict-select-tag v-model="item.exaPapProItemCode" placeholder="请输入项目code(来源于项目模板或项目分配表）"
                                   :dictCode="dictParams.dictExaPapProItemCode" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="分项编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemCode">
                <a-input v-model="item.standItemCode" placeholder="请输入标准分项编码"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="分项名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemName">
                <a-input v-model="item.standItemName" placeholder="请输入分项名称(不及格、及格、良好、优秀等）"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="别名简称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemAliasname">
                <a-input v-model="item.standItemAliasname" placeholder="请输入分项别名简称"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="分数最低分(含)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemLowestScore">
                <a-input-number v-model="item.standItemLowestScore" placeholder="请输入分数最低分(含)" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="分数最高分(不含)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemHighestScore">
                <a-input-number v-model="item.standItemHighestScore" placeholder="请输入分数最高分(不含)" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="分数最低百分比(含)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemLowestRate">
                <a-input-number v-model="item.standItemLowestRate" placeholder="请输入分数最低分率百分比(含)" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="分数最高百分比(不含)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemHighestRate">
                <a-input-number v-model="item.standItemHighestRate" placeholder="请输入分数最高分率百分比(不含)" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="项目序号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemOrder">
                <a-input-number v-model="item.standItemOrder" placeholder="请输入项目序号" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <!--            <a-col :span="12">-->
            <!--              <a-form-model-item label="分项实际得分" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemActualScore">-->
            <!--                <a-input-number v-model="item.standItemActualScore" placeholder="请输入标准分项评分实际得分" style="width: 100%" />-->
            <!--              </a-form-model-item>-->
            <!--            </a-col>-->
            <a-col :span="24">
              <a-form-model-item label="" prop="" style='text-align: center;'>
                <a-button type="dashed" danger style='color: #c21111;' @click="delRowCustom(index)"><a-icon type="delete"/>删除此项</a-button>
              </a-form-model-item>
            </a-col>
          </a-row>
          <!--          <a-row style='border-top: 1px solid #dbdbdb;padding-top: 10px;'>-->
          <!--            <a-col :span="12">-->
          <!--              <a-form-model-item label="特殊限制" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItSpecialRestrictions">-->
          <!--                <a-input-number v-model="model.standItSpecialRestrictions" placeholder="请输入特殊限制（0无限制，1性别，2年级 3年龄，4预留）" style="width: 100%" />-->
          <!--              </a-form-model-item>-->
          <!--            </a-col>-->
          <!--            <a-col :span="12">-->
          <!--              <a-form-model-item label="特殊限制最小值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sitemSpecRestMinvalue">-->
          <!--                <a-input-number v-model="model.sitemSpecRestMinvalue" placeholder="请输入特殊限制数最小值（含即大于等于或等于）" style="width: 100%" />-->
          <!--              </a-form-model-item>-->
          <!--            </a-col>-->
          <!--            <a-col :span="12">-->
          <!--              <a-form-model-item label="特殊限制最大值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sitemSpecRestMaxvalue">-->
          <!--                <a-input-number v-model="model.sitemSpecRestMaxvalue" placeholder="请输入特殊限制数最大值（不含或小于）" style="width: 100%" />-->
          <!--              </a-form-model-item>-->
          <!--            </a-col>-->
          <!--            &lt;!&ndash;          <a-col :span="24">&ndash;&gt;-->
          <!--            &lt;!&ndash;            <a-form-model-item label="是否启用(1是启用，0否停用)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="izOpen">&ndash;&gt;-->
          <!--            &lt;!&ndash;              <a-input-number v-model="model.izOpen" placeholder="请输入是否启用(1是启用，0否停用)" style="width: 100%" />&ndash;&gt;-->
          <!--            &lt;!&ndash;            </a-form-model-item>&ndash;&gt;-->
          <!--            &lt;!&ndash;          </a-col>&ndash;&gt;-->
          <!--            &lt;!&ndash;          <a-col :span="24">&ndash;&gt;-->
          <!--            &lt;!&ndash;            <a-form-model-item label="0表示未删除,1表示删除" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="delFlag">&ndash;&gt;-->
          <!--            &lt;!&ndash;              <a-input-number v-model="model.delFlag" placeholder="请输入0表示未删除,1表示删除" style="width: 100%" />&ndash;&gt;-->
          <!--            &lt;!&ndash;            </a-form-model-item>&ndash;&gt;-->
          <!--            &lt;!&ndash;          </a-col>&ndash;&gt;-->
          <!--          </a-row>-->
        </div>
        <div @click="addRowCustom()" style='margin-top: 20px;text-align: center;'><a-button type="primary" block><a-icon type="add"/>新增分项标准项</a-button></div>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction, postAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { changePassword } from '@api/api'

export default {
  name: 'CallAchStandardItemProjectItemForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data () {
    return {
      model:{
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 9 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
      confirmLoading: false,
      validatorRules: {
      },
      url: {
        standardList: "/relachstandarddistribution/relAchStandardDistribution/getByPaperId",//根据试卷ID 查询对应的试卷标准

        dynamicItemAdd: "/achachievementstandard/achAchievementStandard/dynamicItemAdd",
        edit: "/achachievestandarditem/achAchieveStandardItem/edit",
        queryById: "/achachievestandarditem/achAchieveStandardItem/queryById"
      },
      standardList: [],
      standardModel: {
        subjectId : null,
        izOpen : null,
        sysOrgCode : null,
        delFlag : null,
        paperId: null,
      },
      achievementStandardId: null,
      dictParams: {
        dictExaPapProItemCode: "ach_exam_paper_project_item aeppi,aeppi.exa_pap_pro_item_name,aeppi.id",
      },
      //连续新增标准分项
      //动态增加表单
      orderMainModel: {
        mainCustomerList: [],
      },

    }
  },
  computed: {
    formDisabled(){
      return this.disabled
    },
  },
  created () {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add (record) {
      //试卷标准默认值
      this.standardModel.paperId = record.id
      this.standardModel.subjectId = record.subjectId
      this.standardModel.izOpen = record.izOpen
      this.standardModel.sysOrgCode = record.sysOrgCode
      this.standardModel.delFlag = record.delFlag

      this.getStandardData();
      this.dictParams.dictExaPapProItemCode = "ach_exam_paper_project_item aeppi,aeppi.exa_pap_pro_item_name,aeppi.id,aeppi.examination_paper_id = \'"+this.standardModel.paperId+"\' and aeppi.del_flag  =0 order by aeppi.exa_pap_pro_item_order"
    },
    getStandardData(){
      let params = {
        //关联对象id（1考试卷id(默认)、2分项id、3学生id、4班级id 5年级id 6学校id 7老师id 8科目id）
        relObjId : this.standardModel.paperId,
        //1考试卷id(默认)、2分项id、3学生id、4班级id 5年级id 6学校id 7老师id 8科目id
        relObjType : 1,
      }
      getAction(this.url.standardList,params).then((res)=>{
        if(res.success){
          this.standardList = []
          res.result.forEach(item => {
            this.standardList.push({
              label: item.achievementStandardName,
              value: item.achievementStandardId
            })
          })
        }else{
          this.$message.warning(res.message);
        }
      }).finally(() => {
        this.confirmLoading = false;
        this.close();
      });
    },
    submitForm () {
      const that = this;
      //新增-父级标准数据， 新增-标准分项明细数据
      let params = {
        achAchieveStandardItem : this.orderMainModel.mainCustomerList,//新增-标准分项明细数据
      }
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          postAction(this.url.dynamicItemAdd,params).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }
      });
      //提交完后清空缓存动态表单数据，一面影响下次数据
      this.orderMainModel.mainCustomerList = [{}]
    },

    //连续新增标准分项
    addRowCustom () {
      if (this.achievementStandardId === null){
        alert("请先选择一条试卷对应的标准")
        return
      }
      let addItemData = {}
      addItemData.id = null;
      addItemData.achievementStandardId = this.achievementStandardId;//所选标准
      addItemData.subjectId = this.standardModel.subjectId;
      addItemData.standItemActualScore = null;
      addItemData.standItSpecialRestrictions = null;
      addItemData.sitemSpecRestMinvalue = null;
      addItemData.sitemSpecRestMaxvalue = null;
      addItemData.standItemOrder = 0;
      addItemData.standItemWeightCoefficient = null;
      addItemData.standItemFatherId = "0";
      addItemData.izOpen = 1;
      addItemData.sysOrgCode = this.standardModel.sysOrgCode;
      addItemData.delFlag = 0;
      this.orderMainModel.mainCustomerList.push(addItemData);
      this.$forceUpdate();
    },
    delRowCustom (index) {
      let all = this.orderMainModel
      all['mainCustomerList'].splice(index,1);
      this.orderMainModel.mainCustomerList.splice(index,1);
      this.$forceUpdate();
    },
  }
}
</script>
